﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FytSoa.Common.Utils;
using FytSoa.Common.Captcha;
using FytSoa.Domain.Core.Cache;
using FytSoa.DynamicApi;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace FytSoa.CrossCutting
{
    public static class BootStrapperIoC
    {
        public static void RegisterServices(this IServiceCollection services)
        {
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            // application services
            var assemblyService = Assembly.Load("FytSoa.Application");
            var serviceType = assemblyService.GetTypes().Where(u => u.IsClass && !u.IsAbstract && !u.IsGenericType && u.Name.EndsWith("Service")).ToList();
            foreach (var item in serviceType.Where(s => !s.IsInterface))
            {
                var interfaceType = item.GetInterfaces();
                if (interfaceType.Length == 3) {
                    services.AddScoped(interfaceType[0], item);
                }
                else
                {
                    services.AddScoped(interfaceType[2], item);
                }
            }

            // verify code
            services.AddScoped<SecurityCode>();

            // cache
            services.AddScoped<ICacheService, MemoryService>();
            
            // log
            Logger.AddLogger();

            // id
            Unique.GetInstance();
            
            // dynamic webapi
            services.AddDynamicWebApi();
        }
    }
    
    
}